package org.boot.ssm.wxshop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.UserLevel;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.mapper.UserLevelMapper;
import org.boot.ssm.wxshop.service.UserLevelService;
import org.boot.ssm.wxshop.service.UserService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: wxshop
 * @description: 用户等级实现类
 * @author: 刘涛
 * @create: 2018-03-14 21:17
 **/
@Service
public class UserLevelServiceImpl extends BaseServiceImpl<UserLevel> implements UserLevelService{
    @Autowired
    private UserLevelMapper userLevelMapper;

    @Override
    /*****************************
    * @Description: 分页条件查询所有用户等价 
    * @Param: [pageNum, pageSize, userLevel] 
    * @return: com.github.pagehelper.PageInfo<org.boot.ssm.wxshop.domain.UserLevel> 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public PageInfo<UserLevel> selectAllForPage(Integer pageNum,Integer pageSize,UserLevel userLevel) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<UserLevel> pageInfo = new PageInfo<>(userLevelMapper.selectAllByName(userLevel));
        return pageInfo;
    }

    @Override
    /*****************************
    * @Description: 用户等级验证
    * @Param: [userLevel] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public JsonResult verifyLevelName(UserLevel userLevel) {
        if(userLevel.getId()==null){
            Integer i = this.queryCount(userLevel);
            if (i == 0) {
                return new JsonResult(200, "等级名称名可用");
            } else {
                return new JsonResult(500, "等级名称重复");
            }
        }else {
            UserLevel u = this.queryById(userLevel.getId());
            if (u.getName().equals(userLevel.getName())) {
                return new JsonResult(200, "等级名称名可用");
            } else {
                userLevel.setId(null);
                Integer i = this.queryCount(userLevel);
                if (i == 0) {
                    return new JsonResult(200, "等级名称名可用");
                } else {
                    return new JsonResult(500, "等级名称重复");
                }
            }

        }
    }

    @Override
    /*****************************
    * @Description: 添加或修改用户等级
    * @Param: [userLevel] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public JsonResult addOrUpdateLevel(UserLevel userLevel) {
        Integer isSuccess;
        if (userLevel.getId() == null || userLevel.getId().equals("")) {
            isSuccess = this.saveNoNull(userLevel);
            if (isSuccess == 1) {
                return new JsonResult(200, "添加成功", "user/userLevel/addorupdatelevel");
            } else {
                throw new BusinessException("添加失败");
            }
        } else {
            isSuccess = this.updateNoNull(userLevel);
            if (isSuccess == 1) {
                return new JsonResult(200, "修改成功", "user/userLevel/addorupdatelevel");
            } else {
                throw new BusinessException("修改失败");
            }
        }

    }

    @Override
    /*****************************
    * @Description: 逻辑删除用户等级
    * @Param: [userLevel, count] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public JsonResult isDeleteLevel(UserLevel userLevel,Integer count) {
        if(count>0){
            return new JsonResult(500,"只能删除没有用户使用的等级");
        }else {
            Integer isDelete;
            userLevel.setIsDelete(0);
            isDelete = this.updateDelete(userLevel);
            if (isDelete == 1) {
                return new JsonResult(200, "删除成功");
            } else {
                throw new BusinessException("删除失败");
            }
        }
    }

}
